home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 February / EnigmA AMIGA RUN 04 (1996)(G.R. Edizioni)(IT)[!][issue 1996-02][Skylink CD III].iso / earcd / comm2 / xbtx.lha / Source / Application.cpp next >
C/C++ Source or Header  |  1995-12-03  |  7KB  |  356 lines

  1. /*
  2. **    $Id: Application.cpp 1.7 1995/12/03 12:16:23 olsen Exp olsen $
  3. **
  4. **    :ts=4
  5. */
  6.  
  7. /*
  8.  * Copyright © 1995 by Olaf Barthel, All Rights Reserved
  9.  *
  10.  * Redistribution and use in source and binary forms, with or without
  11.  * modification, are permitted provided that the following conditions
  12.  * are met:
  13.  * 1. Redistributions of source code must retain the above copyright
  14.  *    notice, this list of conditions and the following disclaimer.
  15.  * 2. Redistributions in binary form must reproduce the above copyright
  16.  *    notice, this list of conditions and the following disclaimer in the
  17.  *    documentation and/or other materials provided with the distribution.
  18.  *
  19.  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
  20.  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  21.  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
  22.  * EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23.  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  24.  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  25.  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  26.  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  27.  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  28.  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29.  *
  30.  * This software has not been validated by the ``Bundesamt fuer Zulassungen in
  31.  * der Telekommunikation'' of the ``Deutsche Bundepost Telekom'' and thus
  32.  * must not be used for accessing the BTX-Network of the Telekom in Germany.
  33.  *
  34.  * Diese Software hat keine Zulassung durch das Bundesamt fuer Zulassungen in
  35.  * der Telekommunikation der Deutschen Bundespost Telekom und darf daher nicht
  36.  * am Netz der Deutschen Bundespost Telekom in Deutschland betrieben werden.
  37.  */
  38.  
  39. /****************************************************************************/
  40.  
  41. #include <dos/dosextens.h>
  42.  
  43. #include <clib/exec_protos.h>
  44. #include <clib/dos_protos.h>
  45.  
  46. #ifdef __SASC
  47. #include <pragmas/exec_pragmas.h>
  48. #include <pragmas/dos_pragmas.h>
  49.  
  50. extern struct ExecBase        *SysBase;
  51. extern struct DosLibrary    *DOSBase;
  52. #endif    // _SASC
  53.  
  54. #include <stdio.h>
  55.  
  56. /****************************************************************************/
  57.  
  58. #ifndef _APPLICATION_HPP
  59. #include "Application.hpp"
  60. #endif
  61.  
  62. /****************************************************************************/
  63.  
  64. #define D(x)
  65.  
  66. /****************************************************************************/
  67.  
  68. Application::Application()
  69. {
  70.     AppChannel    = NULL;
  71.     AppService    = NULL;
  72.     AppDisplay    = NULL;
  73.     AppModem    = NULL;
  74. }
  75.  
  76. Application::~Application()
  77. {
  78.     Close();
  79. }
  80.  
  81. VOID Application::Close(VOID)
  82. {
  83.     if(AppDisplay)
  84.     {
  85.         delete AppDisplay;
  86.         AppDisplay = NULL;
  87.     }
  88.  
  89.     if(AppChannel)
  90.     {
  91.         delete AppChannel;
  92.         AppChannel = NULL;
  93.     }
  94.  
  95.     if(AppService)
  96.     {
  97.         delete AppService;
  98.         AppService = NULL;
  99.     }
  100.  
  101.     if(AppModem)
  102.     {
  103.         delete AppModem;
  104.         AppModem = NULL;
  105.     }
  106. }
  107.  
  108. STRPTR Application::Open(STRPTR PubScreen,int XScale,int YScale,BOOL TextOnly,BOOL Direct,CONST STRPTR Channel,ULONG Unit,ULONG Baud,BOOL RTS_CTS)
  109. {
  110.     IOChannel    *ThisChannel;
  111.     BTXDisplay    *ThisDisplay;
  112.     BOOL         IsSerial;
  113.  
  114.     if(ThisChannel = new IOFile)
  115.     {
  116.         IsSerial = FALSE;
  117.  
  118.         if(ThisChannel->Open(Channel))
  119.         {
  120.             delete ThisChannel;
  121.             ThisChannel = NULL;
  122.         }
  123.     }
  124.  
  125.     if(!ThisChannel)
  126.     {
  127.         if(ThisChannel = new IOSerial)
  128.         {
  129.             IsSerial = TRUE;
  130.  
  131.             if(ThisChannel->Open(Channel,Unit,Baud,RTS_CTS))
  132.             {
  133.                 delete ThisChannel;
  134.                 ThisChannel = NULL;
  135.             }
  136.         }
  137.     }
  138.  
  139.     if(ThisChannel)
  140.     {
  141.         if(TextOnly)
  142.             ThisDisplay = new TextDisplay;
  143.         else
  144.             ThisDisplay = new GfxDisplay;
  145.  
  146.         if(ThisDisplay)
  147.         {
  148.             if(ThisDisplay->Open(PubScreen,XScale,YScale,Direct))
  149.             {
  150.                 delete ThisDisplay;
  151.                 ThisDisplay = NULL;
  152.             }
  153.         }
  154.  
  155.         if(!ThisDisplay)
  156.         {
  157.             if(ThisDisplay = new TextDisplay)
  158.             {
  159.                 if(ThisDisplay->Open(PubScreen,XScale,YScale,Direct))
  160.                 {
  161.                     delete ThisDisplay;
  162.                     ThisDisplay = NULL;
  163.                 }
  164.             }
  165.         }
  166.  
  167.         if(ThisDisplay)
  168.         {
  169.             if(IsSerial)
  170.             {
  171.                 if(AppModem = new ModemService)
  172.                 {
  173.                     if(AppModem->Open(ThisChannel,ThisDisplay))
  174.                     {
  175.                         delete AppModem;
  176.                         AppModem = NULL;
  177.                     }
  178.                 }
  179.  
  180.                 if(!AppModem)
  181.                 {
  182.                     delete ThisChannel;
  183.                     ThisChannel = NULL;
  184.  
  185.                     delete ThisDisplay;
  186.                     ThisDisplay = NULL;
  187.  
  188.                     return("Error opening modem service.");
  189.                 }
  190.             }
  191.  
  192.             if(AppService = new BTXService)
  193.             {
  194.                 if(AppService->Open(ThisDisplay,this))
  195.                 {
  196.                     delete AppService;
  197.                     AppService = NULL;
  198.                 }
  199.             }
  200.  
  201.             if(AppService)
  202.             {
  203.                 AppChannel = ThisChannel;
  204.                 AppDisplay = ThisDisplay;
  205.  
  206.                 return(NULL);
  207.             }
  208.             else
  209.             {
  210.                 delete AppModem;
  211.                 AppModem = NULL;
  212.  
  213.                 delete AppChannel;
  214.                 AppChannel = NULL;
  215.  
  216.                 delete AppDisplay;
  217.                 AppDisplay = NULL;
  218.  
  219.                 return("Error opening BTX service.");
  220.             }
  221.         }
  222.         else
  223.         {
  224.             delete ThisChannel;
  225.  
  226.             return("Error opening display.");
  227.         }
  228.     }
  229.     else
  230.         return("Error opening serial channel.");
  231. }
  232.  
  233. LONG Application::DoEvent(VOID)
  234. {
  235.     ULONG Mask;
  236.  
  237.     Mask = AppChannel->WaitMask() | AppDisplay->WaitMask() | SIGBREAKF_CTRL_C;
  238.  
  239.     for(;;)
  240.     {
  241.         while(AppChannel->Waiting() || AppDisplay->Waiting())
  242.         {
  243.             if(AppChannel->Waiting())
  244.             {
  245. #if 0 D(+1)
  246.                 int rc;
  247.                 rc = AppService->ProcessInput();
  248.  
  249.                 if(rc < 0)
  250.                     return(-1);
  251.  
  252.                 if(rc > 0)
  253.                 {
  254.                     UBYTE matrix[40*24],line[44];
  255.                     int x,y,i;
  256.  
  257.                     AppService->GetMatrix((UBYTE *)matrix,&x,&y);
  258.  
  259.                     printf("+----------------------------------------+ %ld,%ld\n",x,y);
  260.                     for(i = 0 ; i < 24 ; i++)
  261.                     {
  262.                         memcpy(line,&matrix[40 * i],40);
  263.                         line[40] = 0;
  264.                         printf("|%s|\n",line);
  265.                     }
  266.                     printf("+----------------------------------------+\n\n");
  267.                 }
  268. #else
  269.                 if(AppService->ProcessInput() < 0)
  270.                     return(-1);
  271. #endif
  272.             }
  273.  
  274.             if(AppDisplay->Waiting())
  275.             {
  276.                 LONG Char = AppDisplay->GetChar();
  277.  
  278.                 if(Char >= 0)
  279.                 {
  280.                     if(Char == '\033')
  281.                         return(-1);
  282.                     else
  283.                         AppChannel->PutChar(Char);
  284.                 }
  285.             }
  286.         }
  287.  
  288.         if(Wait(Mask) & SIGBREAKF_CTRL_C)
  289.             return(-1);
  290.     }
  291. }
  292.  
  293. VOID Application::WaitForUserInput(VOID)
  294. {
  295.     ULONG Signals;
  296.  
  297.     AppDisplay->PutLine("[Hit any key to exit]");
  298.  
  299.     for(;;)
  300.     {
  301.         Signals = Wait(AppDisplay->WaitMask() | SIGBREAKF_CTRL_C);
  302.  
  303.         if(Signals & AppDisplay->WaitMask())
  304.         {
  305.             if(AppDisplay->Waiting())
  306.             {
  307.                 if(AppDisplay->GetChar() >= 0)
  308.                     break;
  309.             }
  310.         }
  311.  
  312.         if(Signals & SIGBREAKF_CTRL_C)
  313.             break;
  314.     }
  315. }
  316.  
  317. VOID Application::WaitEvent(VOID)
  318. {
  319.     Wait(AppChannel->WaitMask() | AppDisplay->WaitMask() | SIGBREAKF_CTRL_C);
  320. }
  321.  
  322. LONG Application::DispatchEvent(VOID)
  323. {
  324.     LONG Value;
  325.  
  326.     Value = DispatchDisplayEvent();
  327.  
  328.     if(Value == CHANNELERR_Nothing)
  329.         Value = (LONG)AppChannel->GetChar();
  330.  
  331.     return(Value);
  332. }
  333.  
  334. LONG Application::DispatchDisplayEvent(VOID)
  335. {
  336.     if(SetSignal(0,0) & SIGBREAKF_CTRL_C)
  337.         return(CHANNELERR_EOF);
  338.  
  339.     if(AppDisplay->Waiting())
  340.     {
  341.         LONG Char;
  342.  
  343.         Char = AppDisplay->GetChar();
  344.  
  345.         if(Char == '\033')
  346.             return(CHANNELERR_EOF);
  347.         else
  348.         {
  349.             if(Char >= 0)
  350.                 AppChannel->PutChar(Char);
  351.         }
  352.     }
  353.  
  354.     return(CHANNELERR_Nothing);
  355. }
  356.